package com.zdoon.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zdoon.mapper.ClazzMapper;
import com.zdoon.pojo.Clazz;
import com.zdoon.pojo.ClazzQueryParam;
import com.zdoon.pojo.PageResult;
import com.zdoon.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

//    分页查询班级表
    @Override
    public PageResult<Clazz> page(ClazzQueryParam clazzQueryParam) {
//        设置pageHelper
        PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());
//        查询数据
        List<Clazz> clazzList = clazzMapper.selectClazzList(clazzQueryParam);
//        封装数据
        Page<Clazz> clazzPage = (Page<Clazz>) clazzList;

        PageResult<Clazz> clazzPageResult = new PageResult<>(clazzPage.getTotal(), clazzPage.getResult());

        return clazzPageResult;

    }


    @Override
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    @Override
    public void add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.add(clazz);
    }

    @Override
    public Clazz getById(Integer id) {
        Clazz clazz = clazzMapper.getById(id);
        return clazz;
    }

    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    @Override
    public List<Clazz> list() {
        List<Clazz> clazzList = clazzMapper.selectAllClazzs();
        return clazzList;
    }
}
